package structure

import "fmt"

type CircularQueue struct {
	data []int
	head int
	tail int
	n    int
}

/**
 * 循环队列
 * @since 2022/8/1 14:20
 * @author 李磊
 */
func newCircularQueue(len int) *CircularQueue {
	queue := &CircularQueue{}
	data := make([]int, len)
	queue.data = data
	queue.n = len
	return queue
}
func (q *CircularQueue) in(i int) {
	if q.head == (q.tail+1)%q.n {
		fmt.Println("队列已满")
		return
	}
	q.data[q.tail] = i
	q.tail = (q.tail + 1) % q.n
}

func (q *CircularQueue) out() int {
	if q.head == q.tail {
		fmt.Println("队空")
		return -1
	}
	out := q.data[q.head]
	q.head = (q.head + 1) % q.n
	return out
}
